package com.egzosn.service.pay.handler;

import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayMessage;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.exception.PayErrorException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 微信支付回调处理器
 * Created by egan on 2016/6/1.
 */
@Component
public class WxPayMessageHandler extends BasePayMessageHandler {




//    @Transactional
    @Override
    public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {

        //交易状态
        Map<String, Object> message = payMessage.getPayMessage();

        //交易状态
        if ("SUCCESS".equals(message.get("result_code"))){
            BigDecimal payAmount = ((BigDecimal) payMessage.getTotalFee()).divide(new BigDecimal(100));
            handle( payMessage.getOutTradeNo(), payAmount, message.toString(),  (Integer) payService.getPayConfigStorage().getAttach());
            return payService.successPayOutMessage(payMessage);
        }

        return  payService.getPayOutMessage("FAIL", "失败");
    }
}
